﻿using DimensionData.Data2;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CPPEI.PM.Shell.Views
{
    public partial class WorkSheetView
    {
        #region work item
        public DTree LoadWorkItem2Tree(WorkSheet workSheet)
        {
            PMServiceRef.PMWebServiceSoapClient client = new PMServiceRef.PMWebServiceSoapClient();

            List<DTable> dTableList = new List<DTable>();

            // 获取工作表引用的工作项目类别
            PMServiceRef.WorkItemCategoryRef rootWorkItemCategory = client.GetWorkItemCategory4WorkSheet(workSheet.Id);

            // 添加到项目上
            UIContext.Instance.Project.WorkItemCategoryList.Add(ModelUtil.Convert(rootWorkItemCategory));

            DCTable dcTable = new DCTable();
            dcTable.Combine(new DTable(rootWorkItemCategory.Code, rootWorkItemCategory.Caption));

            PMServiceRef.WorkItemCategoryRef nextWorkItemCategory = client.GetNextWorkItemCategory4WorkSheet(workSheet.Id, rootWorkItemCategory.Id);
            while (nextWorkItemCategory != null)
            {
                // 添加到项目上
                UIContext.Instance.Project.WorkItemCategoryList.Add(ModelUtil.Convert(nextWorkItemCategory));

                dcTable.Combine(new DTable(nextWorkItemCategory.Code, nextWorkItemCategory.Caption));

                nextWorkItemCategory = client.GetNextWorkItemCategory4WorkSheet(workSheet.Id, nextWorkItemCategory.Id);
            }

            // 构建dTree
            DTree dTree = LoadDTree(workSheet, rootWorkItemCategory, null);
            dTree.Table = dcTable;
            return dTree;
        }

        public DTree LoadDTree(WorkSheet workSheet,
            PMServiceRef.WorkItemCategoryRef workItemCategory,
            PMServiceRef.WorkItemRef workItemRef)
        {
            Guid pWorkItemId = Guid.Empty;
            if (workItemRef != null)
            {
                pWorkItemId = workItemRef.Id;
            }

            DTree dTree = new DTree(workItemCategory.Code, workItemCategory.Level);

            PMServiceRef.PMWebServiceSoapClient client = new PMServiceRef.PMWebServiceSoapClient();

            PMServiceRef.WorkItemRef[] workItemRefArray = client.GetWorkItemList4WorkSheet(workSheet.Id, workItemCategory.Id, pWorkItemId);
            for (int index = 0; index < workItemRefArray.Length; index++)
            {
                UIContext.Instance.Project.WorkItemList.Add(ModelUtil.Convert(workItemRefArray[index]));

                DTreeNode dTreeNode = new DTreeNode();
                dTreeNode.Code = workItemRefArray[index].Code;
                dTreeNode.Name = workItemRefArray[index].Caption;
                dTreeNode.Level = workItemRefArray[index].Level;

                dTree.Nodes.Add(dTreeNode);
                dTreeNode.Tree = dTree;

                LoadDTreeNode(workSheet, dTreeNode, workItemCategory, workItemRefArray[index]);

                //dTreeNode.NextTree = LoadDTree(workSheet, workItemCategory
            }

            return dTree;
        }

        public void LoadDTreeNode(WorkSheet workSheet, DTreeNode pDTreeNode, 
            PMServiceRef.WorkItemCategoryRef workItemCategory,
            PMServiceRef.WorkItemRef pWorkItemRef)
        {
            PMServiceRef.PMWebServiceSoapClient client = new PMServiceRef.PMWebServiceSoapClient();

            PMServiceRef.WorkItemRef[] workItemRefArray = client.GetWorkItemList4WorkSheet(workSheet.Id, workItemCategory.Id, pWorkItemRef.Id);
            for (int index = 0; index < workItemRefArray.Length; index++)
            {
                DTreeNode dTreeNode = new DTreeNode();
                dTreeNode.Code = workItemRefArray[index].Code;
                dTreeNode.Name = workItemRefArray[index].Caption;
                dTreeNode.Level = workItemRefArray[index].Level;

                pDTreeNode.Nodes.Add(dTreeNode);
                dTreeNode.Tree = pDTreeNode.Tree;
                dTreeNode.ParentNode = pDTreeNode;
            }
        }
        #endregion

        #region work stage
        public DTree LoadWorkStage2Tree(WorkSheet workSheet)
        {
            PMServiceRef.PMWebServiceSoapClient client = new PMServiceRef.PMWebServiceSoapClient();
            PMServiceRef.WorkStageRef[] workStageList = client.GetWorkStageList4WorkSheet(workSheet.Id, Guid.Empty);


            List<DTable> dTableList = new List<DTable>();

            // 获取工作表引用的工作项目类别
            PMServiceRef.WorkStageRef rootWorkStageRef = workStageList[0];

            DCTable dcTable = new DCTable();
            dcTable.Combine(new DTable(rootWorkStageRef.Code, rootWorkStageRef.Caption));

            PMServiceRef.WorkStageItemRef[] workStageItemArray = client.GetWorkStageItemList4WorkSheet(workSheet.Id, rootWorkStageRef.Id, Guid.Empty);

            DTree dTree = new DTree(rootWorkStageRef.Code, rootWorkStageRef.Level);
            dTree.Table = dcTable;
            for (int index = 0; index < workStageItemArray.Length; index++)
            {
                DTreeNode dTreeNode = new DTreeNode();
                dTreeNode.Code = workStageItemArray[index].Code;
                dTreeNode.Name = workStageItemArray[index].Caption;
                dTreeNode.Level = workStageItemArray[index].Level;

                dTree.Nodes.Add(dTreeNode);
                dTreeNode.Tree = dTree;
            }

            return dTree;
        }

        #endregion

        public void Refresh()
        {
            PMServiceRef.PMWebServiceSoapClient client = new PMServiceRef.PMWebServiceSoapClient();
            // 获取服务器上的工作表信息
            PMServiceRef.WorkSheet workSheet = client.GetWorkSheet(UIContext.Instance.ProjectId, this.WorkSheetId);

            _currentWorkSheet = new WorkSheet();
            _currentWorkSheet.Id = workSheet.Id;
            _currentWorkSheet.Caption = workSheet.Caption;
            _currentWorkSheet.Changeset = 1;

            _currentWorkSheet.DDoc = new DimensionData.Data2.DDoc();
            _currentWorkSheet.DDoc.YTree = this.LoadWorkItem2Tree(_currentWorkSheet);
            _currentWorkSheet.DDoc.XTree = this.LoadWorkStage2Tree(_currentWorkSheet);

            _currentWorkSheet.BuildData();

            AttachData(_currentWorkSheet);

            BindToDataGrid(_currentWorkSheet);
        }
    }
}
